package com.cuanbo.dualscreenteachzhu.service;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.cuanbo.dualscreenteachzhu.base.App;
import com.cuanbo.dualscreenteachzhu.utils.AppUtil;
import com.cuanbo.dualscreenteachzhu.utils.LogUtil;

public class BackgroundService extends Service{
    private int num=0;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {
        notification();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (App.running) {
                    try{
                        work();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    num++;
                    if (num == Long.MAX_VALUE) {
                        num = 0;
                    }
                    try{
                        Thread.sleep(1000);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        return Service.START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 后台运行需要在通知栏提醒，否则会报错
     */
    public void notification() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            nm.createNotificationChannel(new NotificationChannel("42", "智慧教学机", NotificationManager.IMPORTANCE_DEFAULT));
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "42");
            startForeground(2, builder.build());
        }
    }

    public static long getDataRecordTime=0;

    public static boolean haveHdmiIn=false;

    private void work() throws Exception{
        AppUtil.timer(this);
        //每隔60秒检查一次web服务器访问是否异常
        /*if (num != 0 && num % 60 == 0) {
            new HttpWebServerCheck().request();
        }*/
        //检查是否有HDMI信号
        if(num!=0&&num%5==0){
            CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
            String[] cameraIds = cameraManager.getCameraIdList();
            boolean have=false;
            for(int i=0;i<cameraIds.length;i++){
                Log.i("摄像头"+i,cameraIds[i]);
                if(cameraIds[i].equals("10")){
                    have=true;
                }
            }
            if(haveHdmiIn==false&&have){
                LogUtil.add("","有HDMI信号，刷新界面","摄像头");
                Intent intent = new Intent("com.cuanbo.dralscreenteachzhu.action");
                intent.putExtra("type", 6);
                App.getInstance().sendBroadcast(intent);
            }
            haveHdmiIn=have;
        }
    }


}
